//
//  SubTableView.m
//
//
//  Created by xu on 15/11/8.
//
//

#import "ThemeFBTableView.h"
#import "BaseViewController.h"
#import "ThemeFBCell.h"
#import "Util.h"

#import "NSString+WPAttributedMarkup.h"
#import "WPAttributedStyleAction.h"
#import "WPHotspotLabel.h"

#import "SJAvatarBrowser.h"
#import "UIImageView+WebCache.h"
@implementation ThemeFBTableView
{
    AFHTTPRequestOperationManager *_manager;
    float allHeight;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        [self _createView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self _createView];
    }
    return self;
}


- (void)_createView
{
    self.dataSource = self;
    self.delegate = self;
    self.userInteractionEnabled = NO;
    self.tableFooterView.hidden = YES;
    
}

//  父类中已经添加了tableview的代理
-(void)initData{
    _nilArray = [[NSMutableArray alloc] init];
    _manager = [AFHTTPRequestOperationManager manager];
    //  NSString *url=[NSString stringWithFormat:@"%@chat/getCollection",BaseURL];
    NSDictionary *parameters = @{@"memberid":@17,@"size":@"20",@"endid":@"0"};
    AFHTTPRequestOperation*op= [_manager POST:@"http://testapp.1000fishing.com/api/chat/getCollection"  parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError*error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        NSData *jsonData = [[dic valueForKey:@"obj"] dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *result  = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"%@",dic);
        if (error==nil&&![result isEqual:[NSNull null]]&&result!=nil&&result.count!=0) {
            for (int i = 0; i<[result count]; i++) {
                NSMutableDictionary  *c= result[i];
                
                UILabel *hc=[[UILabel alloc]initWithFrame:CGRectZero];
                hc.text=c[@"content"];
                NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
                paragraph.alignment = NSLineBreakByWordWrapping;
                NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13], NSParagraphStyleAttributeName: paragraph};
                CGSize labSize;
                if (kScreenWidth==320) {
                    labSize =CGSizeMake(207, 9999);
                }else if (kScreenWidth==375){
                    labSize =CGSizeMake(245, 9999);
                }else if (kScreenWidth==414){
                    labSize =CGSizeMake(335, 9999);
                }else if (kScreenWidth>414){
                    labSize =CGSizeMake(500, 9999);
                }
                
                CGSize size = [@"公司有无英文" boundingRectWithSize:labSize options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
                allHeight=allHeight+size.height+10;
                [c setObject:[NSNumber numberWithFloat:size.height+10] forKey:@"textheight"];
                [_nilArray addObject:c];
            }
        }
        [self reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }];
    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    [op start];
}
 //实现TABLE方法

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nilArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    NSDictionary* style1 = @{@"body":MidGray,
    //                             @"name1":[WPAttributedStyleAction styledActionWithAction:^{
    //                                 NSLog(@"Help action");
    //                             }],
    //                             @"name2":[WPAttributedStyleAction styledActionWithAction:^{
    //                                 NSLog(@"Settings action");
    //                             }],
    //                             @"link": PinkColor};
    ThemeFBCell * cell = [ThemeFBCell createWithTableView:tableView];
    //        cell.content.attributedText = [@"<name1>啊啊啊</name1><body>回复</body><name2>啊啊啊</name2> <body>公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称公司有无英文简称</body>  " attributedStringWithStyleBook:style1];
    NSDictionary *ic = [_nilArray objectAtIndex:indexPath.row];
//    UILabel *hc=[[UILabel alloc]initWithFrame:CGRectZero];
//    hc.text=ic[@"content"];
    cell.content.attributedText=[@"公司有无英文" attributedStringWithStyleBook:nil];
//    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//    paragraph.alignment = NSLineBreakByWordWrapping;
//    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13], NSParagraphStyleAttributeName: paragraph};
//    CGSize size = [hc.text boundingRectWithSize:CGSizeMake(290, 9999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    CGRect rect = cell.frame;
    rect.size.height =  [ic[@"textheight"] floatValue];
    cell.frame = rect;
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        //end of loading
        dispatch_async(dispatch_get_main_queue(),^{
            if (([self.heightDataDelegate respondsToSelector:@selector(sendheightData:)])) {
                [self.heightDataDelegate sendheightData:allHeight];
            }
        });
    }
}

@end
